package com.yrmao.yrmtimedialog.ui.widget;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yrmao.yrmtimedialog.App;
import com.yrmao.yrmtimedialog.R;
import com.yrmao.yrmtimedialog.ui.adapter.MTimeAdapter;
import com.yrmao.yrmtimedialog.ui.models.MTimeDataBean;
import com.yrmao.yrmtimedialog.ui.widget.interfaces.MTimeOnClickListener;
import com.yrmao.yrmtimedialog.utils.GetTimeUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 自定义时间对话框
 * <p>
 * Created by yemao on 16/11/23.
 */

public class MTimeDialog implements View.OnClickListener, MTimeAdapter.OnTimeItemListener {
    private AlertDialog.Builder dialog;
    private Activity context;
    private RecyclerView mRListview;
    private View dialog_view;
    public static int TIME_DAY = 1;
    public static int TIME_MONTH = 2;
    public static int TIME_YEAY = 3;
    private GridLayoutManager gridmanger;
    public static int typeTime = TIME_DAY;//时间类型

    private MTimeAdapter mAdapter;
    private ImageView iv_up_time, iv_down_time;//左右点击按钮
    private TextView tv_content;//中间时间
    private TextView tv_year;//顶部时间
    private int atday = 1, atmonth = 11, atyear = 2016; //当前年份/月份/日期
    private int cheackday, cheackmonth, cheackyear;//选中年份/月份/日期
    int changeMonth;//点击确定时间
    int changeYear;
    int changDay;
    private List<MTimeDataBean> dataMonth = new ArrayList<>();
    private List<MTimeDataBean> dataDay = new ArrayList<>();
    private List<MTimeDataBean> dataYear = new ArrayList<>();
    private List<MTimeDataBean> data = new ArrayList<>();
    private MTimeOnClickListener mMTimeOnClickListener;

    public MTimeDialog(Activity context) {
        this.context = context;
        initTime();
        initDayData();
        initMonthData();
        initView();
    }

    private void initTime() {
        Calendar now = Calendar.getInstance();


        atyear = now.get(Calendar.YEAR);
        atmonth = now.get(Calendar.MONTH) + 1;
        atday = now.get(Calendar.DAY_OF_MONTH);


        cheackday = atday;
        cheackmonth = atmonth;
        cheackyear = atyear;

        changeMonth = cheackmonth;
        changeYear = cheackyear;
        changDay = cheackday;
    }

    private void initView() {
        dialog_view = LayoutInflater.from(App.getApp()).inflate(R.layout.time_dialog, null);
        iv_up_time = (ImageView) dialog_view.findViewById(R.id.iv_up_time);
        iv_down_time = (ImageView) dialog_view.findViewById(R.id.iv_down_time);
        tv_content = (TextView) dialog_view.findViewById(R.id.tv_content);
        mRListview = (RecyclerView) dialog_view.findViewById(R.id.rw_time_dialog);
        tv_year = (TextView) dialog_view.findViewById(R.id.tv_year);
        gridmanger = new GridLayoutManager(context, 7);
        mRListview.setLayoutManager(gridmanger);
        mAdapter = new MTimeAdapter(data, context);
        mRListview.setAdapter(mAdapter);
        iv_up_time.setOnClickListener(this);
        iv_down_time.setOnClickListener(this);
        mAdapter.setOnTimeItemListener(this);

    }

    /**
     * 根据时间类型设置数据
     *
     * @param type
     */
    private void setTimeType(int type) {
        if (type == TIME_DAY) {
            gridmanger.setSpanCount(7);
            mRListview.setVisibility(View.VISIBLE);
            setDayData(changDay);
            data.clear();
            data.addAll(dataDay);
            mAdapter.notifyDataSetChanged();
            tv_content.setText(cheackyear + "年" + cheackmonth + "月");
            tv_year.setText(cheackyear + "年" + cheackmonth + "月" + cheackday + "日");
        } else if (type == TIME_MONTH) {
            gridmanger.setSpanCount(6);
            mRListview.setVisibility(View.VISIBLE);
            setMonthData(cheackmonth);
            data.clear();
            data.addAll(dataMonth);
            mAdapter.notifyDataSetChanged();
            tv_content.setText(cheackyear + "年");
            tv_year.setText(cheackyear + "年" + cheackmonth + "月");

        } else {
            tv_content.setText(cheackyear + "年");
            mRListview.setVisibility(View.GONE);
            tv_year.setText(cheackyear + "年");
        }
        typeTime = type;
    }

    public void showDayTime() {
        initView();
        setTimeType(TIME_DAY);
        dialog = new AlertDialog.Builder(context);
        dialog.setView(dialog_view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                cheackday = changDay;
                cheackmonth = changeMonth;
                cheackyear = changeYear;
                if (mMTimeOnClickListener != null)
                    mMTimeOnClickListener.getYear_Month_Day(cheackyear, cheackmonth, cheackday);
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                changDay = cheackday;
                changeMonth = cheackmonth;
                changeYear = cheackyear;

            }
        });
        dialog.show();
    }

    public void showMonthTime() {
        initView();
        setTimeType(TIME_MONTH);
        dialog = new AlertDialog.Builder(context);
        dialog.setView(dialog_view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                cheackmonth = changeMonth;
                cheackyear = changeYear;
                if (mMTimeOnClickListener != null)
                    mMTimeOnClickListener.getYear_Month(cheackyear, cheackmonth);
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                changeMonth = cheackmonth;
                changeYear = cheackyear;

            }
        });
        dialog.show();
    }

    public void showYearTime() {
        initView();
        setTimeType(TIME_YEAY);
        dialog = new AlertDialog.Builder(context);
        dialog.setView(dialog_view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                cheackyear = changeYear;
                if (mMTimeOnClickListener != null)
                    mMTimeOnClickListener.getYear(cheackyear);
            }
        });
        dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                changeYear = cheackyear;
            }
        });
        dialog.show();
    }

    public void setMTimeOnClickListener(MTimeOnClickListener mlistener) {
        mMTimeOnClickListener = mlistener;
    }

    /**
     * 初始化日期时间
     */
    private void initDayData() {
        int size = GetTimeUtil.getCurrentMonthDay();
        MTimeDataBean dataBean;
        int week = GetTimeUtil.getWeekOfDate(changeYear, changeMonth);
        for (int i = 1; i <= size + 2; i++) {
            dataBean = new MTimeDataBean();
            dataBean.week = week;
            if (atday == i) {
                dataBean.setMTimeData(i, true, true);
            } else {
                dataBean.setMTimeData(i, false, false);
            }
            dataDay.add(dataBean);
        }
    }

    /**
     * 初始化月
     */
    private void initMonthData() {
        dataMonth.clear();
        MTimeDataBean dataBean;
        for (int i = 1; i <= 12; i++) {

            dataBean = new MTimeDataBean();
            if (atmonth == i) {
                dataBean.setMTimeData(i, true, true);
            } else {
                dataBean.setMTimeData(i, false, false);
            }
            dataMonth.add(dataBean);
        }
    }

    /**
     * 设置天
     * index代表号
     *
     * @param index
     */
    private void setDayData(int index) {
        dataDay.clear();
        MTimeDataBean dataBean;
        int week = GetTimeUtil.getWeekOfDate(changeYear, changeMonth);
        for (int i = 1; i <= GetTimeUtil.getDaysByYearMonth(changeYear, changeMonth); i++) {
            dataBean = new MTimeDataBean();
            dataBean.week = week;
            if (atday == i && i == index) {
                dataBean.setMTimeData(i, true, true);
            } else if (atday == i && i != index) {
                dataBean.setMTimeData(i, true, false);
            } else if (atday != i && i == index) {
                dataBean.setMTimeData(i, false, true);
            } else {
                dataBean.setMTimeData(i, false, false);
            }
            dataDay.add(dataBean);
        }
    }

    /**
     * 设置月
     * index代表月分
     *
     * @param index
     */
    private void setMonthData(int index) {
        dataMonth.clear();
        MTimeDataBean dataBean;

        for (int i = 1; i <= 12; i++) {
            dataBean = new MTimeDataBean();

            if (atmonth == i && i == index) {
                dataBean.setMTimeData(i, true, true);
            } else if (atmonth == i && i != index) {
                dataBean.setMTimeData(i, true, false);
            } else if (atmonth != i && i == index) {
                dataBean.setMTimeData(i, false, true);
            } else {
                dataBean.setMTimeData(i, false, false);
            }
            dataMonth.add(dataBean);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_up_time://点击左

                if (typeTime == TIME_DAY) {
                    changeMonth--;
                    if (changeMonth <= 0) {
                        changeMonth = 12;
                        changeYear--;
                    }
                    setDayData(changDay);
                    data.clear();
                    data.addAll(dataDay);
                    mAdapter.notifyDataSetChanged();
                    tv_year.setText(changeYear + "年" + changeMonth + "月" + changDay + "日");
                    tv_content.setText(changeYear + "年" + changeMonth + "月");
                } else if (typeTime == TIME_MONTH) {
                    changeYear--;
                    tv_content.setText(changeYear + "年");
                    tv_year.setText(changeYear + "年" + changeMonth + "月");

                } else if (typeTime == TIME_YEAY) {
                    changeYear--;
                    tv_content.setText(changeYear + "年");
                    tv_year.setText(changeYear + "年");
                }

                break;
            case R.id.iv_down_time://点击右
                if (typeTime == TIME_DAY) {
                    changeMonth++;
                    if (changeMonth >= 13) {
                        changeMonth = 1;
                        changeYear++;
                    }
                    setDayData(changDay);
                    data.clear();
                    data.addAll(dataDay);
                    mAdapter.notifyDataSetChanged();
                    tv_content.setText(changeYear + "年" + changeMonth + "月");
                    tv_year.setText(changeYear + "年" + changeMonth + "月" + changDay + "日");
                } else if (typeTime == TIME_MONTH) {
                    changeYear++;
                    tv_content.setText(changeYear + "年");
                    tv_year.setText(changeYear + "年" + changeMonth + "月");
                } else if (typeTime == TIME_YEAY) {
                    changeYear++;
                    tv_content.setText(changeYear + "年");
                    tv_year.setText(changeYear + "年");
                }

                break;
        }
    }

    @Override
    public void cheackItem(int position, int time) {
        if (typeTime == TIME_MONTH) {
            changeMonth = time;
            setMonthData(position + 1);
            data.clear();
            data.addAll(dataMonth);
            tv_year.setText(changeYear + "年" + time + "月");
            tv_content.setText(changeYear + "年");
        } else if (typeTime == TIME_DAY) {
            changDay = time;
            setDayData(position + 1);
            data.clear();
            data.addAll(dataDay);
            tv_year.setText(changeYear + "年" + changeMonth + "月" + time + "日");
            tv_content.setText(changeYear + "年" + changeMonth + "月");
        }
        mAdapter.notifyDataSetChanged();
    }
}
